package com.demo.file;

import bsh.commands.dir;
import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Comparator;

public class UncompressFile {

    @Test
    public void copy() throws IOException {
        FileUtils.copyDirectoryToDirectory(new File("D:\\data\\testClasses\\" + "com"), new File("E:\\data\\uncompressClasses\\passenger\\"));

    }

    @Test
    public void copyJarToDir() throws IOException {
        FileUtils.copyFileToDirectory(new File("D:\\projects\\20220330Project\\learn-test\\target\\connectDBTest-0.0.1-SNAPSHOT.jar"), new File("D:\\data\\logs\\"));

    }



    /**
     * 删除某个目录下的所有文件 并删除该目录
     */
    @Test
    public void deleteDirAllFile(){
        String directoryPath = "D:\\data\\testClasses";  // 替换为你的目录路径
        try {
            Files.walk(Paths.get(directoryPath))
                    .sorted(Comparator.reverseOrder())
                    .forEach(path -> {
                        try {
                            Files.deleteIfExists(path);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            String jarDirPath = "D:\\data\\testClasses\\";
            String jarName = "passenger-management-bootstrap-3.1.2-SNAPSHOT.jar";
            File dir = new File(jarDirPath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            String jarPath = jarDirPath + jarName;
            StringBuilder cmd = new StringBuilder();
            cmd.append("cmd /c cd " + jarDirPath + " && jar -xvf " + jarPath);
            Process exec = Runtime.getRuntime().exec(String.valueOf(cmd));

            //输出是为了解决卡住问题
            InputStream in = exec.getInputStream();
            InputStreamReader inReader = new InputStreamReader(in, StandardCharsets.UTF_8);
            BufferedReader inBr = new BufferedReader(inReader);
            String strIn;
            while ((strIn = inBr.readLine()) != null) {
                System.out.println(strIn);
            }

            FileUtils.copyDirectoryToDirectory(new File(jarDirPath + File.separator + "BOOT-INF" + File.separator + "classes" + File.separator + "com"), new File(jarDirPath));

            //true 代表递归 false 代表不递归
            Collection<File> files = FileUtils.listFiles(new File(jarDirPath + "BOOT-INF" + File.separator + "lib" + File.separator), null, false);
            StringBuilder executeCmd = new StringBuilder();
            executeCmd.append("cmd /c cd " + jarDirPath);
            for (File file : files) {
                System.out.println(file.getName());
                if (file.getName().contains("passenger")) {
                    FileUtils.copyFileToDirectory(file, new File(jarDirPath));
                    executeCmd.append(" &&  jar -xvf " + jarDirPath + File.separator + file.getName());
                }
            }
            System.out.println("executeCmd命令：" + executeCmd.toString());
            Process result = Runtime.getRuntime().exec(String.valueOf(executeCmd));

            InputStream inputStream = result.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String strInput;
            while ((strInput = bufferedReader.readLine()) != null) {
                //输出是为了获取最新的文件
                System.out.println("准备复制=" + strInput);
            }

            //复制字节码文件到其他的地方
            FileUtils.copyDirectoryToDirectory(new File(jarDirPath + "com"), new File("E:\\data\\uncompressClasses\\passenger\\"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
